/*
 * sevensegment.c
 *
 * Created: 22.06.2021 22:05:13
 *  Author: Sergey Sozykin
 */ 

#define 	F_CPU   8000000UL

#include "sevensegment.h"


void show_digit(char c, int k)
{
	PORTB &= 0b11110000;
	PORTB |= (1 << k);
	
	PORTD = 0;
	
	switch (c)
	{
		case '0':
			PORTD |= (1<<1) |(1<<2) |(1<<3) |(1<<4) |(1<<5) |(1<<6);
			break;
		case '1': 
			PORTD |= (1<<4) | (1<<5);
			break;
		case '2':
			PORTD |= (1<<0) |(1<<2) |(1<<3) |(1<<5) |(1<<6);
			break;
		case '3':
			PORTD |= (1<<0) |(1<<3) |(1<<4) |(1<<5) |(1<<6);
			break;
		case '4':
			PORTD |= (1<<0) |(1<<1) |(1<<4) |(1<<5);
			break;
		case '5':
			PORTD |= (1<<0) |(1<<1) |(1<<3) |(1<<4) |(1<<6);
			break;
		case '6':
			PORTD |= (1<<0) |(1<<1)  |(1<<2) |(1<<3) |(1<<4) |(1<<6);
			break;
		case '7':
			PORTD |= (1<<4) | (1<<5)| (1<<6);
			break;
		case '8':
			PORTD |= (1<<0) |(1<<1) |(1<<2) |(1<<3) |(1<<4) |(1<<5) |(1<<6);
			break;
		case '9':
			PORTD |= (1<<0) |(1<<1) |(1<<3) |(1<<4) |(1<<5) |(1<<6);
			break;
	}
}


void show_integer(int n)
{
	int n4 = n % 10;
	int n3 = (n / 10) % 10;
	int n2 = (n / 100) % 10;
	int n1 = n / 1000;
	show_digit('0'+n1,0);
	_delay_ms(5);
	show_digit('0'+n2,1);
	_delay_ms(5);
	show_digit('0'+n3,2);
	_delay_ms(5);
	show_digit('0'+n4,3);
	_delay_ms(5);	
}